package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApUserBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApUserBehaviorServiceImpl implements ApUserBehaviorService {
    @Autowired
    private CacheService cacheService;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 用户点赞
     *
     * @param dto
     */
    @Override
    public ResponseResult likesBeanvior(LikesBehaviorDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String key = BehaviorConstants.LIKE_BEHAVIOR + dto.getArticleId();
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        if (dto.getOperation() == 0) {
            Object obj = cacheService.hGet(key, user.getId().toString());
            if (obj != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已点赞");
            }
            log.info("保存当前key:{},{}", key, user.getId().toString());
            cacheService.hPut(key, user.getId().toString(), JSON.toJSONString(dto));
            mess.setAdd(1);
        } else {
            log.info("删除当前key:{},{}", key, user.getId().toString());
            cacheService.hDelete(key, user.getId().toString());
            mess.setAdd(-1);
        }
        //发送消息，数据聚合
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 用户阅读
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBeanvior(ReadBehaviorDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String key = BehaviorConstants.READ_BEHAVIOR + dto.getArticleId();
        Object obj = cacheService.hGet(key, user.getId().toString());
        if (obj != null) {
            ReadBehaviorDto readBehaviorDto = JSON.parseObject(obj.toString(), ReadBehaviorDto.class);
            readBehaviorDto.setCount(readBehaviorDto.getCount() + dto.getCount());
            log.info("更新当前key:{},{}", key, user.getId().toString());
            cacheService.hPut(key, user.getId().toString(), JSON.toJSONString(readBehaviorDto));
        } else {
            log.info("新增当前key:{},{}", key, user.getId().toString());
            cacheService.hPut(key, user.getId().toString(), JSON.toJSONString(dto));
        }
        //发送消息，数据聚合
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setAdd(1);
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 用户不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikesBeanvior(UnLikesBehaviorDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String key = BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId();
        if (dto.getType() == 0) {
            Object obj = cacheService.hGet(key, user.getId().toString());
            if (obj != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "已点击不喜欢");
            }
            log.info("保存当前key:{},{}", key, user.getId().toString());
            cacheService.hPut(key, user.getId().toString(), JSON.toJSONString(dto));
        } else {
            log.info("删除当前key:{},{}", key, user.getId().toString());
            cacheService.hDelete(key, user.getId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
